#!/usr/bin/python
#Filename : backup_ver5.py

import os
import time

# 1. The files and directories to be backed up are specified int a list.
source = ['/home/hcy/test/python/9_data_struct', '/home/hcy/test/python/8_model']
comment_src = raw_input('Enter you directory :')
if len(comment_src) == 0 :
	srcdir = source
else:
	srcdir = source
	srcdir.append(comment_src)

print 'source dirctory is', srcdir
# 2. The backup must be stored int a main backup directory
target_dir = '/home/hcy/test/python/backup/'
#Rememober to change this to what you will be using

# 3.The Files are backed up into a zip file.
# 4.The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%y%m%d')
# The current time is the name of the ziip archive
now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0 : #check if a comment was entered
#	target = today +os.sep + now + '.zip'
	target = today +os.sep + now + '.tar.gz'
	print 1
else :
#	target = today +os.sep + now + '_' + comment.replace(' ', '_') + '.zip'
	target = today +os.sep + now + '_' + comment.replace(' ', '_') + '.tar.gz'
	print 2

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
	os.mkdir(today)  # make directory
	print 'Successfully created directory', today


# 5. We use the zip command (in unix/linux )to put the files in a zip archive

#zip_command = "zip -qr  '%s'  %s"%(target, ' '.join(srcdir))
tar_command = "tar -cvzf %s %s -X  /home/hcy/test/python/7_function/def.py"%(target, ' '.join(srcdir))

#Run the backup

#if os.system(zip_command) == 0:
if os.system(tar_command) == 0:
	print 'successful backup to', target
else:
	print 'backup falled'
